namespace Sessionizer.Core.Unity
{
    public sealed class Sessions<T> : ISessions<T>
    {
        private volatile ISessionsFactory<T> _factory;
        private readonly object _syncRoot;
        private T _instance;
        public Sessions(ISessionsFactory<T> factory)
        {
            _factory = factory;
            _syncRoot = new object();
        }

        public T Instance
        {
            get
            {
                if (!ReferenceEquals(_factory, null))
                {
                    lock (_syncRoot)
                    {
                        if (!ReferenceEquals(_factory, null))
                        {
                            _instance = _factory.Create();
                            _factory = null;
                        }
                    }
                }
                return _instance;
            }
        }
    }
}